<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array(		
	);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.$_RQ['nickname'].'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['account'])){
		$params['account like']='%'.$_RQ['account'].'%';
		$return['params']['account']=$_RQ['account'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('mid','email','avatar','account','nickname','createtime','reg_ip','sex','mobile');
	$list=pdo_getslice('jy_jdpm_member',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:m:s',$item['createtime']);
			$item['email']=$item['email']?$item['email']:'无';	
			$item['nickname']=$item['nickname']?$item['nickname']:'云拍校尉';			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'用户管理'),
			array('op'=>'edit','title'=>'添加用户'),
		),
		'list'=>array('key'=>'mid','data'=>'list','delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'mid','title'=>'mid','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'account','title'=>'用户账号','type'=>'string'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'email','title'=>'邮箱','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'reg_ip','title'=>'登录ip','type'=>'string'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'string'),
			
		
		),'action'=>array(	
			array('op'=>'sendsms','title'=>'发送站内信','type'=>'modal','plugin'=>'jy_jdpm'),
			//array('type'=>'href','op'=>'source','title'=>'他的推广'),
			array('op'=>'credit','title'=>'余额/信誉','type'=>'modal','plugin'=>'jy_jdpm'),	
			array('type'=>'post','op'=>'del','title'=>'删除','message'=>'确定删除?'),			
		),),
		'search'=>array(
			array('type'=>'string','title'=>'会员UID','name'=>'mid','help'=>''),
			array('type'=>'string','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','title'=>'用户账号','name'=>'account','help'=>''),
			array('type'=>'string','title'=>'昵称','name'=>'nickname','help'=>''),
		),
	);
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['mid']);
	if($id){
	$item=pdo_get('jy_jdpm_member',array('mid'=>$id));
	$item['pcd']=array('province'=>$item['prov'],'city'=>$item['city'],'district'=>$item['district']);
	
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['verify']=array('0'=>'未认证','1'=>'已认证');
	$return['check']=array('0'=>'未申请认证','1'=>'待认证','2'=>'已认证','3'=>'未通过');
	$return['sex']=array('0'=>'女','1'=>'男');
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['pcd']=cfc('tools')->pcd();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回会员列表'),
			
		),
		'desc'=>'会员信息设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','name'=>'account','title'=>'登录账号','help'=>'不可重复'),
					array('type'=>'string','name'=>'newpwd','title'=>'密码','help'=>'不修改留空'),
					array('type'=>'string','name'=>'newpaypwd','title'=>'支付密码','help'=>'不修改留空'),
					array('type'=>'image','name'=>'avatar','title'=>'头像','help'=>'图片尺寸：正方形图片'),
					array('type'=>'string','name'=>'nickname','title'=>'昵称'),
					array('type'=>'string','name'=>'scorebuy','title'=>'买家得分','help'=>'重置满多少可用, 空或0 不限制'),
					array('type'=>'createtime','name'=>'birthday','title'=>'生日'),
					array('type'=>'radio','name'=>'sex','title'=>'性别','options'=>'sex'),
					array('type'=>'string','name'=>'email','title'=>'邮箱'),
					array('type'=>'radio','name'=>'verify_email','title'=>'邮箱认证','options'=>'verify'),
					array('type'=>'string','name'=>'mobile','title'=>'手机号'),
					array('type'=>'radio','name'=>'verify_mobile','title'=>'手机号认证','options'=>'verify'),
					array('type'=>'string','name'=>'phone','title'=>'固定电话'),
					array('type'=>'string','name'=>'postalcode','title'=>'邮编'),
					array('type'=>'pcd','name'=>'pcd','title'=>'所在地','options'=>'pcd'),
					array('type'=>'string','name'=>'address','title'=>'街道地址','help'=>'不需要写省市区, 必须大于五个字符'),	
					array('type'=>'radio','name'=>'status','title'=>'状态','options'=>'status'),						
					
		)
	),
			array(
				'title'=>'实名认证',
				'list'=>array(
					
					array('type'=>'string','name'=>'truename','title'=>'真实姓名'),
					array('type'=>'string','name'=>'idcard','title'=>'身份证号'),
					array('type'=>'image','name'=>'idcard_front','title'=>'身份证正面照片'),
					array('type'=>'image','name'=>'idcard_behind','title'=>'身份证反面照片'),
					array('type'=>'radio','name'=>'idcard_check','options'=>'check','title'=>'实名认证'),					
				)
			),
			array(
				'title'=>'卖家信息',
				'list'=>array(
					array('type'=>'string','name'=>'score','title'=>'卖家得分'),
					array('type'=>'string','name'=>'title','title'=>'店铺名称'),
					array('type'=>'string','name'=>'desc','title'=>'店铺简介'),
					array('type'=>'image','name'=>'logo','title'=>'店铺logo','help'=>'图片尺寸：正方形图片'),
								
				)
			)
	)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['mid']);
	
	$a=pdo_getcolumn('jy_jdpm_member',array('account'=>trim($post['account'])),'mid');
	if($a && $id!=$a){
		exi('用户名重复，请重新输入','error');
	}
	$data=array_fields(array('account','avatar','postalcode','nickname','scorebuy','birthday','sex','email','verify_email','mobile','mobile_email',
	'phone','address','status','truename','idcard','idcard_front','idcard_behind','idcard_check','score'),$post);
	$data['prov']=$post['pcd']['province'];$data['city']=$post['pcd']['city'];$data['district']=$post['pcd']['district'];
	if($post['newpwd']){
		$data['pwd']=$post['newpwd'];
	}
	if($post['newpaypwd']){
		$data['paypwd']=$post['newpaypwd'];
	}
	if($id){
		pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
		exi('修改成功','','list');
	}else{
		$data['createtime']=time();
		pdo_insert('jy_jdpm_member',$data);
		exi('添加成功','','list');
	}
	
}
if($_RQ['op']=='sendsms'){
	$id=intval($_RQ['mid']);
	$item['id']=$id;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'站内信',
		'edit'=>array(
			'0'=>array('type'=>'text','title'=>'消息内容','name'=>'sms'),
			
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	
	$data=array('content'=>$post['sms'],'type'=>'系统提示','createtime'=>time(),'mid'=>$id);
	pdo_insert('jy_jdpm_mysms',$data);
	exi('发送成功','',manage_url('jy_jdpm/membermoney/',true));
}
if($_RQ['op']=='credit'){
	$id=intval($_RQ['mid']);
	$item=pdo_get('jy_jdpm_member',array('mid'=>$id));
	$aliv=$item['wallet_pledge']-$item['wallet_pledge_freeze'];
	$aliv=$aliv?sprintf("%.2f", $aliv):0.00;
	$item['credit']='当前：'.$item['wallet_pledge'].'     冻结：'.$item['wallet_pledge_freeze'].'     可用：'.$aliv;
	$alivlim=$item['wallet_limsum']-$item['wallet_limsum_freeze'];
	$alivlim=$alivlim?sprintf("%.2f", $alivlim):0.00;
	$item['xy']='当前：'.$item['wallet_limsum'].'     冻结：'.$item['wallet_limsum_freeze'].'     可用：'.$alivlim;
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'account',
		'desc'=>'账户信息编辑',
		'edit'=>array(
			array('type'=>'string','title'=>'用户账号','read'=>true,'name'=>'account'),
			array('type'=>'string','title'=>'账户余额','read'=>true,'name'=>'credit'),
			array('type'=>'string','title'=>'信誉额度','read'=>true,'name'=>'xy'),
			array('type'=>'radio','title'=>'操作项','name'=>'do','options'=>'do'),
			array('type'=>'radio','title'=>'动作','name'=>'act','options'=>'act'),
			array('type'=>'string','title'=>'输入金额','name'=>'money'),
			array('type'=>'string','title'=>'备注','name'=>'remark'),
		)
	);
	$return['do']=array('1'=>'账户余额','2'=>'信用额度');
	$return['act']=array('1'=>'加','2'=>'减','3'=>'冻结','4'=>'解冻');
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='account'){
	$post=get_postdata();
	$id=intval($post['mid']);
	if(!$id){
		exi('参数错误！','error');
	}
	$item=pdo_get('jy_jdpm_member',array('mid'=>$id));
	if(!$post['do'] || !$post['act'] || !$post['money']){
		exi('操作项，动作和金额都需要填写','error');
	}
	$no='plg'.time().rand(10000,99999);
	if($post['do']==1){
		if($post['act']==1){
			$post['money']=intval($post['money']);
			$data['wallet_pledge']=$post['money']+$item['wallet_pledge'];
			$aliv=$data['wallet_pledge']-$item['wallet_pledge_freeze'];
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_deposit','createtime'=>time(),'annotation'=>$post['remark'],'income'=>$post['money'],'balance'=>$data['wallet_pledge'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_pledge_bill',$data2);
		}
		if($post['act']==2){
			$post['money']=intval($post['money']);
			$aliv=$item['wallet_pledge']-$item['wallet_pledge_freeze'];
			if($aliv<$post['money']){
				exi('当前可用余额不足'.$post['money'],'error');
			}
			$aliv=$aliv-$post['money'];
			$data['wallet_pledge']=$item['wallet_pledge']-$post['money'];			
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_deduct','createtime'=>time(),'annotation'=>$post['remark'],'expend'=>$post['money'],'balance'=>$data['wallet_pledge'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_pledge_bill',$data2);
		}
		if($post['act']==3){
			$post['money']=intval($post['money']);
			$aliv=$item['wallet_pledge']-$item['wallet_pledge_freeze'];
			if($aliv<$post['money']){
				exi('当前可用余额不足'.$post['money'],'error');
			}
			$aliv=$aliv-$post['money'];
			$data['wallet_pledge_freeze']=$item['wallet_pledge_freeze']+$post['money'];			
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_freeze','createtime'=>time(),'annotation'=>$post['remark'],'expend'=>$post['money'],'balance'=>$item['wallet_pledge'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_pledge_bill',$data2);
		}
		if($post['act']==4){
			$post['money']=intval($post['money']);
			$aliv=$item['wallet_pledge']-$item['wallet_pledge_freeze'];
			if($item['wallet_pledge_freeze']<$post['money']){
				exi('当前可解冻余额不足'.$post['money'],'error');
			}
			$aliv=$aliv+$post['money'];
			$data['wallet_pledge_freeze']=$item['wallet_pledge_freeze']-$post['money'];			
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_unfreeze','createtime'=>time(),'annotation'=>$post['remark'],'income'=>$post['money'],'balance'=>$item['wallet_pledge'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_pledge_bill',$data2);
		}
		
	}elseif($post['do']==2){
		if($post['act']==1){
			$post['money']=intval($post['money']);
			$data['wallet_limsum']=$post['money']+$item['wallet_limsum'];
			$aliv=$data['wallet_limsum']-$item['wallet_limsum_freeze'];
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_deposit','createtime'=>time(),'annotation'=>$post['remark'],'income'=>$post['money'],'balance'=>$data['wallet_limsum'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_limsum_bill',$data2);
		}
		if($post['act']==2){
			$post['money']=intval($post['money']);
			$aliv=$item['wallet_limsum']-$item['wallet_limsum_freeze'];
			if($aliv<$post['money']){
				exi('当前可用余额不足'.$post['money'],'error');
			}
			$aliv=$aliv-$post['money'];
			$data['wallet_limsum']=$item['wallet_limsum']-$post['money'];			
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_deduct','createtime'=>time(),'annotation'=>$post['remark'],'expend'=>$post['money'],'balance'=>$data['wallet_limsum'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_limsum_bill',$data2);
		}
		if($post['act']==3){
			$post['money']=intval($post['money']);
			$aliv=$item['wallet_limsum']-$item['wallet_limsum_freeze'];
			if($aliv<$post['money']){
				exi('当前可用余额不足'.$post['money'],'error');
			}
			$aliv=$aliv-$post['money'];
			$data['wallet_limsum_freeze']=$item['wallet_limsum_freeze']+$post['money'];			
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_freeze','createtime'=>time(),'annotation'=>$post['remark'],'expend'=>$post['money'],'balance'=>$item['wallet_limsum'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_limsum_bill',$data2);
		}
		if($post['act']==4){
			$post['money']=intval($post['money']);
			$aliv=$item['wallet_limsum']-$item['wallet_limsum_freeze'];
			if($item['wallet_limsum_freeze']<$post['money']){
				exi('当前可解冻余额不足'.$post['money'],'error');
			}
			$aliv=$aliv+$post['money'];
			$data['wallet_limsum_freeze']=$item['wallet_limsum_freeze']-$post['money'];			
			pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
			$data2=array('mid'=>$id,'order_no'=>$no,'changetype'=>'admin_unfreeze','createtime'=>time(),'annotation'=>$post['remark'],'income'=>$post['money'],'balance'=>$item['wallet_limsum'],'usable'=>$aliv);
			pdo_insert('jy_jdpm_member_limsum_bill',$data2);
		}
	}

	exi('操作成功','',manage_url('jy_jdpm/membermoney/',true));
}
if($_RQ['op']=='realname'){
	$params=array(		
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('mid','avatar','account','nickname','truename','idcard','idcard_check');
	$list=pdo_getslice('jy_jdpm_member',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:m:s',$item['createtime']);
			$item['email']=$item['email']?$item['email']:'无';				
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'用户管理'),
			array('op'=>'edit','title'=>'添加用户'),
		),
		'list'=>array('key'=>'mid','data'=>'list','delall'=>false,'edit'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'mid','title'=>'mid','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'account','title'=>'用户账号','type'=>'string'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'truename','title'=>'姓名','type'=>'string'),
			array('name'=>'idcard','title'=>'身份证号','type'=>'string'),
			array('name'=>'idcard_check','title'=>'认证状态','type'=>'radio','options'=>'check'),
			
			
			'action'=>array(	
			array('op'=>'check','title'=>'实名认证','type'=>'modal','plugin'=>'jy_jdpm'),
				
		),
		)),
	);
	$return['check']=array('0'=>'未申请认证','1'=>'待认证','2'=>'已认证','3'=>'未通过');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='check'){
	$id=intval($_RQ['mid']);
	$item=pdo_get('jy_jdpm_member',array('mid'=>$id),array('mid','avatar','account','nickname','truename','idcard','idcard_check','idcard_front','idcard_behind'));
	$htmls=array(
		'type'=>'edit',
		'post'=>'docheck',
		'desc'=>'实名认证',
		'edit'=>array(
			array('type'=>'string','title'=>'登录账号','name'=>'account','read'=>true),
			array('type'=>'string','title'=>'真实姓名','name'=>'truename'),
			array('type'=>'string','title'=>'身份证号','name'=>'idcard'),
			array('type'=>'image','title'=>'身份证正面照片','name'=>'idcard_front'),
			array('type'=>'image','title'=>'身份证反面照片','name'=>'idcard_behind'),
			array('name'=>'idcard_check','title'=>'实名认证','type'=>'radio','options'=>'check'),
			
		)
	);
	$return['check']=array('0'=>'未申请认证','1'=>'待认证','2'=>'已认证','3'=>'未通过');
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='docheck'){
	$post=get_postdata(); 
	$id=intval($post['mid']);
	if(!$id){
		exi('参数错误！','error');
	}	
	$data=array('truename'=>trim($post['truename']),'idcard'=>trim($post['idcard']),'idcard_front'=>$post['idcard_front'],'idcard_behind'=>$post['idcard_behind'],'idcard_check'=>$post['idcard_check'],);
	pdo_update('jy_jdpm_member',$data,array('mid'=>$id));
	$mobile = pdo_getcolumn('jy_jdpm_member',array('mid'=>$id),'mobile');
	if($mobile){
		//$this->model('notice')->set_sms($mobile,'您的身份证验证成功!',4);
	}
	if($post['idcard_check'] == 3){
		$msg = '实名验证审核失败!请重新提交！';
	}
	if($post['idcard_check'] == 2){
		$msg = '您的身份证验证成功!';
	}
	$this->model('notice')->set_websms($msg,0,$id,'jy_jdpm/member.safety/idcard/');
	
	exi('操作成功');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['mid']);
	if($id){
		pdo_delete('jy_jdpm_member',array('mid'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jdpm_member',array('mid'=>$id));
			}
		}
	}
	exi('删除成功！');
}